<template>
    <div class="components_edit_modal">
        <el-dialog title="发起流程" :visible.sync="state" :close-on-click-modal="false" append-to-body>
            <el-form :model="form"   :rules="rules" ref="form">
                 <el-form-item label="流程标题：" class="lineblock">
                      {{ form.subject }}
                </el-form-item>

                <el-form-item label="当前节点：">
                     {{ nextProcess.currentNodeName }}
                </el-form-item>
                <el-form-item label="下级节点：">
                    {{ nextProcess.nodeName }}
                </el-form-item>
                
                <el-form-item label="审批人：" prop="toUserId">
                    <el-select v-model="form.toUserId" placeholder="请选择审批人">
                        <el-option
                            v-for="item in userList" :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="审批意见：">
                    <el-input
                            type="textarea"
                            :maxlength="500"
                            :autosize="{ minRows: 2, maxRows: 4}"
                            placeholder="请输入审批意见"
                            v-model="form.optionContent">
                    </el-input>
                </el-form-item>
               
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submit" size="small">确 定</el-button>
                <el-button @click="state = false" size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    import { mapGetters } from 'vuex'
    export default {
        data() {
            return {
                rules: {
                    subject: [
                        { required: true, message: '请填写流程标题', trigger: 'blur', type: 'string'},
                    ],
                    toUserId: [
                        { required: true, message: '请选择下级审批人', trigger: 'change', type: 'number'},

                    ],
                },
                form: {
                    businessCode: null,
                    businessId: null,
                    subject: null,   // 流程标题
                    toUserId:null,   // 提交下级审批人
                    optionContent: null,  //审批意见
                },
                userList: null,
                nextProcess: {},
                // 业务逻辑相关参数
                businessForm: null,
                businessUrl: null,
                businessActionType: null    // 业务操作类型
            }
        },
        created() {},
        mounted() {},
        computed:{
            state: {
                get: function () {
                    if(this.$store.getters.getFlowInit.state == true){
                        // 处理和组装数据
                        let data = this.$store.getters.getFlowInit.data;
                        this.businessActionType = data.actionType;
                        // 清空数据
                        this.form.optionContent = null;
                        // 发起流程所需参数值
                        this.form.subject = data.flowSubject;
                        this.form.businessCode = data.businessCode;
                        this.form.businessId = data.businessId;
                        // 业务逻辑所需参数
                        this.businessForm = data.businessForm == null ? null : JSON.parse(JSON.stringify(data.businessForm));
                        this.businessUrl = data.businessUrl;
                        // 获取流程下级节点
                        this.getNextNode();
                        this.$nextTick(function(){
                            this.$refs['form'].resetFields();
                        })
                    }
                    return this.$store.getters.getFlowInit.state;
                },
                set: function (v) {
                   if(!v){
                        this.$store.commit("FLOW_INIT", {dialogState: v, data: null});
                   }
                }
            }
        },
        methods: {
            // 打开模态框
            showDialog(flowSubject, businessCode, businessId, businessForm, businessUrl){
                // 清空数据
                this.form.optionContent = null;
                // 打开弹出框
                this.state = true;
                // 发起流程所需参数值
                this.form.subject = flowSubject;
                this.form.businessCode = businessCode;
                this.form.businessId = businessId;
                // 业务逻辑所需参数
                this.businessForm = JSON.parse(JSON.stringify(businessForm));
                this.businessUrl = businessUrl;
                // 获取流程下级节点
                this.getNextNode();
                this.$nextTick(function(){
                    this.$refs['form'].resetFields();
                })
            },
            // 查询流程下级节点
            getNextNode() {
                let vm = this;
                vm.axios.get(api.flow.queryBeginNextProcess, {
                    params:{
                        businessCode: vm.form.businessCode
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.nextProcess = JSON.parse(JSON.stringify(response.data.data));
                        // 查询下级审批人列表
                        this.getUserList();
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             // 获取用户集合
            getUserList() {
                let vm = this;
                vm.axios.get(api.queryUserListByRoleFlag, {
                    params:{
                        roleFlag: vm.nextProcess.userRule
                    }
                }).then((response) => {
                    if (response.data.success) {
                        this.userList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 保存
            submit() {
                let vm = this;
                vm.$refs['form'].validate((valid) => {
                    if (valid) {
                        if(vm.businessActionType == "save"){
                            vm.submitBusiness();
                        }else if(vm.businessActionType == "update"){
                            vm.updateBusiness();
                        }else if(vm.businessActionType == "sumbitFlow"){
                            vm.submitFlow();
                        }
                    } else {
                        return false;
                    }
                });

            },
            // 提交业务相关代码
            submitBusiness(){
                let vm = this;
                // 业务逻辑请求
                vm.axios.post(vm.businessUrl, vm.businessForm)
                .then((response) => {
                    if (response.data.success) {
                        vm.form.businessId = response.data.data;
                        vm.submitFlow();
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 更新业务操作
            updateBusiness(){
                let vm = this;
                 // 业务逻辑请求
                vm.axios.put(vm.businessUrl, vm.businessForm)
                .then((response) => {
                    if (response.data.success) {
                        vm.submitFlow();
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            // 提交流程
            submitFlow(){
                let vm = this;
                // 请求
                vm.axios.post(api.flow.initFlow, vm.form)
                .then((response) => {
                        if (response.data.success) {
                            vm.$message({
                                type: 'success',
                                message: response.data.msg,
                            });
                            // 关闭弹出窗口
                            vm.state = false;
                            vm.$parent.state = false;    // 关闭父级窗口
                            vm.$parent.$parent.closeAllModal();
                        } else {
                            vm.$message.error(response.data.msg);
                        }
                })
            }
        },
    };
</script>
<style>
</style>